/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * QLMUONSACH.java
 *
 * Created on Oct 20, 2010, 9:37:17 PM
 */

package qltv;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author DELL
 */
public class QLMUONSACH extends javax.swing.JFrame {

    Home home;//=new Home();
    /** Creates new form QLMUONSACH */
    public QLMUONSACH() {
        initComponents();
        loadqlms();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        PNktdg = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        tb_qlms_madg = new javax.swing.JTextField();
        bt_qlms_ktdg = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        tb_qlms_hoten = new javax.swing.JTextField();
        tb_qlms_doituong = new javax.swing.JTextField();
        tb_qlms_diachi = new javax.swing.JTextField();
        tb_qlms_ngaysinh = new javax.swing.JTextField();
        tb_qlms_gioitinh = new javax.swing.JTextField();
        tb_qlms_ngayhethan = new javax.swing.JTextField();
        jLabel40 = new javax.swing.JLabel();
        PNkts = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        tb_qlms_mapm = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        tb_qlms_mas = new javax.swing.JTextField();
        bt_qlms_ktsach = new javax.swing.JButton();
        jLabel19 = new javax.swing.JLabel();
        tb_qlms_tensach = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        tb_qlms_tacgia = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        tb_qlms_linhvuc = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        tb_qlms_ngonngu = new javax.swing.JTextField();
        jLabel23 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        tb_qlms_gia = new javax.swing.JTextField();
        tb_qlms_nhaxuatban = new javax.swing.JTextField();
        bt_qlms_chomuon = new javax.swing.JButton();
        bt_qlms_huythaotac = new javax.swing.JButton();
        btthoat = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tb_xemkq = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 0, 204));

        jLabel9.setText("Mã độc giả");

        bt_qlms_ktdg.setText("Kiểm tra độc giả");
        bt_qlms_ktdg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_qlms_ktdgActionPerformed(evt);
            }
        });

        jLabel12.setText("........................................................................");

        jLabel14.setText("Họ Tên");

        jLabel15.setText("Đối Tượng");

        jLabel16.setText("Địa Chỉ");

        jLabel17.setText("Ngày Sinh");

        jLabel18.setText("Giới Tính");

        jLabel40.setText("Ngày hết hạn");

        javax.swing.GroupLayout PNktdgLayout = new javax.swing.GroupLayout(PNktdg);
        PNktdg.setLayout(PNktdgLayout);
        PNktdgLayout.setHorizontalGroup(
            PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PNktdgLayout.createSequentialGroup()
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addComponent(tb_qlms_madg, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(bt_qlms_ktdg)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PNktdgLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel12)
                .addGap(20, 20, 20))
            .addGroup(PNktdgLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel16)
                    .addComponent(jLabel15)
                    .addComponent(jLabel14)
                    .addComponent(jLabel17)
                    .addComponent(jLabel18)
                    .addComponent(jLabel40))
                .addGap(27, 27, 27)
                .addGroup(PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PNktdgLayout.createSequentialGroup()
                        .addGroup(PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tb_qlms_gioitinh)
                            .addComponent(tb_qlms_ngaysinh, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tb_qlms_doituong)
                            .addComponent(tb_qlms_diachi))
                        .addGap(120, 120, 120))
                    .addGroup(PNktdgLayout.createSequentialGroup()
                        .addGroup(PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tb_qlms_hoten, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tb_qlms_ngayhethan, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        PNktdgLayout.setVerticalGroup(
            PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PNktdgLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(tb_qlms_madg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_qlms_ktdg))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(tb_qlms_hoten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel15)
                    .addComponent(tb_qlms_doituong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(tb_qlms_diachi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(tb_qlms_ngaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel18)
                    .addComponent(tb_qlms_gioitinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktdgLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tb_qlms_ngayhethan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel40))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel10.setText("Mã Phiếu Mượn");

        jLabel11.setText("Mã sách");

        jLabel13.setText("........................................................................");

        bt_qlms_ktsach.setText("Kiểm tra sách");
        bt_qlms_ktsach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_qlms_ktsachActionPerformed(evt);
            }
        });

        jLabel19.setText("Tên Sách");

        jLabel20.setText("Tác Giả");

        jLabel21.setText("Lĩnh Vực");

        jLabel22.setText("Ngôn Ngữ");

        jLabel23.setText("Giá");

        jLabel24.setText("Nhà Xuất bản");

        javax.swing.GroupLayout PNktsLayout = new javax.swing.GroupLayout(PNkts);
        PNkts.setLayout(PNktsLayout);
        PNktsLayout.setHorizontalGroup(
            PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PNktsLayout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tb_qlms_mapm, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel11)
                .addGap(12, 12, 12)
                .addComponent(tb_qlms_mas, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bt_qlms_ktsach))
            .addGroup(PNktsLayout.createSequentialGroup()
                .addGroup(PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PNktsLayout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(jLabel13))
                    .addGroup(PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, PNktsLayout.createSequentialGroup()
                            .addComponent(jLabel24)
                            .addGap(52, 52, 52)
                            .addComponent(tb_qlms_nhaxuatban, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, PNktsLayout.createSequentialGroup()
                            .addGroup(PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel19)
                                .addComponent(jLabel20)
                                .addComponent(jLabel21)
                                .addComponent(jLabel22)
                                .addComponent(jLabel23))
                            .addGap(69, 69, 69)
                            .addGroup(PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(tb_qlms_gia)
                                .addComponent(tb_qlms_ngonngu)
                                .addComponent(tb_qlms_linhvuc)
                                .addComponent(tb_qlms_tacgia)
                                .addComponent(tb_qlms_tensach, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        PNktsLayout.setVerticalGroup(
            PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PNktsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tb_qlms_mas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_qlms_ktsach)
                    .addComponent(jLabel10)
                    .addComponent(tb_qlms_mapm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel19)
                    .addComponent(tb_qlms_tensach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel20)
                    .addComponent(tb_qlms_tacgia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel21)
                    .addComponent(tb_qlms_linhvuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel22)
                    .addComponent(tb_qlms_ngonngu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tb_qlms_gia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel23))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PNktsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel24)
                    .addComponent(tb_qlms_nhaxuatban, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bt_qlms_chomuon.setText("Cho Mượn");
        bt_qlms_chomuon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_qlms_chomuonActionPerformed(evt);
            }
        });

        bt_qlms_huythaotac.setText("Hủy Thao Tác");
        bt_qlms_huythaotac.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_qlms_huythaotacActionPerformed(evt);
            }
        });

        btthoat.setText("Thoát");
        btthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btthoatActionPerformed(evt);
            }
        });

        tb_xemkq.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Mã Sách", "Tên Sách", "Ngày Mượn"
            }
        ));
        jScrollPane1.setViewportView(tb_xemkq);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(PNktdg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(PNkts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(bt_qlms_huythaotac)
                                    .addComponent(bt_qlms_chomuon, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(41, 41, 41)
                                .addComponent(btthoat)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(PNkts, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(PNktdg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(bt_qlms_chomuon)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bt_qlms_huythaotac)
                        .addGap(18, 18, 18)
                        .addComponent(btthoat)))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private final String server = "THAOLE-VAIO";
    private final int port = 1433;
    private final String databaseName = "QLTHUVIEN_Nhom7_DACNPM";
    private final String username = "test";
    private final String password = "123456";
    Connection con = null;
    boolean ktdg=false;
    boolean kts=false;
    String ngaymuon = "",ngayhethantra="",phieutam;
     public void loadqlms()
    {
        bt_qlms_ktsach.setVisible(false);
        tb_qlms_mas.setVisible(false);
        bt_qlms_chomuon.setVisible(false);
    }

    public void cleartextbandoc()
    {
        tb_qlms_hoten.setText("");
        tb_qlms_doituong.setText("");
        tb_qlms_diachi.setText("");
        tb_qlms_ngaysinh.setText("");
        tb_qlms_gioitinh.setText("");
        tb_qlms_mapm.setText("");
        tb_qlms_ngayhethan.setText("");
    }

    public void cleartextsach()
    {
        tb_qlms_tensach.setText("");
        tb_qlms_tacgia.setText("");
        tb_qlms_linhvuc.setText("");
        tb_qlms_gia.setText("");
        tb_qlms_ngonngu.setText("");
        tb_qlms_nhaxuatban.setText("");
    }

    public void layphieumuon()
    {
        phieutam="";
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            try {
                Connection con = DriverManager.getConnection(getURLConnection());
                Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                                     ResultSet.CONCUR_READ_ONLY);
                ResultSet rs=stm.executeQuery("SELECT maphieu FROM phieumuon");
                rs.last();
                String s = rs.getString("maphieu");
                phieutam=s;
                s=s.substring(2,6);
                int i=Integer.parseInt(s);
                i++;
                if (phieutam.equals("")) { phieutam = "PM0001"; }
                else
                {
                    if (i < 10)
                    {
                        s = "PM000" + i;
                    }
                    else if (i < 100)
                    {
                        s = "PM00" + i;
                    }
                    else if (i < 1000)
                    {
                        s = "PM0" + i;
                    }
                    else if (i < 10000)
                    {
                        s = "PM" + i;
                    }
                    phieutam = s;
                }
                tb_qlms_mapm.setText(phieutam);
                tb_qlms_mas.setText("nhập mã sách");
                tb_qlms_madg.setVisible(true);

            } catch (SQLException ex) {
                Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void load_table()
    {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            try {
                Connection con = DriverManager.getConnection(getURLConnection());
                Statement stm =con.createStatement();
                int i=0;
                ResultSet rs=stm.executeQuery("SELECT SACH.MASACH,SACH.TENSACH,PHIEUMUON.NGAYMUON FROM DOCGIA,PHIEUMUON,SACH_PHIEU,SACH WHERE DOCGIA.MADOCGIA='"+ tb_qlms_madg.getText() +"' AND DOCGIA.MADOCGIA=PHIEUMUON.MADOCGIA AND PHIEUMUON.MAPHIEU=SACH_PHIEU.MAPHIEU AND SACH_PHIEU.MASACH = SACH.MASACH AND SACH_PHIEU.NGAYTRA IS NULL");
                while(rs.next())
                {
                    tb_xemkq.setValueAt(rs.getString("MASACH"), i, 0);
                    tb_xemkq.setValueAt(rs.getString("TENSACH"), i, 1);
                    tb_xemkq.setValueAt(rs.getDate("NGAYMUON"), i, 2);
                    i++;
                }

                if(i<5)
                {
                    for(;i<5;i++)
                    {
                        tb_xemkq.setValueAt("", i, 0);
                        tb_xemkq.setValueAt("", i, 1);
                        tb_xemkq.setValueAt("", i, 2);
                    }
                }
            } catch (SQLException ex) {
                Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 
    
    String ngayhethan;
    //ngay thang nam hien tai va het han
    int monthhh, dayhh, yearhh, month, year, day;
    int sltoida=0, soluongmuon = 0;
    boolean taopm=true;
    public void muontoida()
    {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            try {
                Connection con = DriverManager.getConnection(getURLConnection());
                Statement stm= con.createStatement();
                ResultSet rs=stm.executeQuery("select SOLUONGMUONTOIDA FROM DOCGIA,LOAIDOCGIA WHERE DOCGIA.MADOCGIA='" + tb_qlms_madg.getText() + "' AND DOCGIA.MALOAIDOCGIA=LOAIDOCGIA.MALOAIDOCGIA");
                while(rs.next())
                {
                    sltoida=rs.getInt("SOLUONGMUONTOIDA");
                }
            } catch (SQLException ex) {
                Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void bt_qlms_ktdgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_qlms_ktdgActionPerformed
        // TODO add your handling code here:
        cleartextbandoc();
        bt_qlms_chomuon.setVisible(false);
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            try {
                Connection con = DriverManager.getConnection(getURLConnection());
                Statement stm=con.createStatement();
                ResultSet rs = stm.executeQuery("SELECT HOTEN,DIACHI,GIOITINH,TENLOAIDG,NGAYSINH FROM DOCGIA,LOAIDOCGIA WHERE DOCGIA.MADOCGIA='" + tb_qlms_madg.getText() + "' AND DOCGIA.MALOAIDOCGIA=LOAIDOCGIA.MALOAIDOCGIA");
                while(rs.next()) {
                    tb_qlms_hoten.setText(rs.getString("HOTEN"));
                    tb_qlms_diachi.setText(rs.getString("DIACHI"));
                    tb_qlms_gioitinh.setText(rs.getString("GIOITINH"));
                    tb_qlms_doituong.setText(rs.getString("TENLOAIDG"));
                    Date date= rs.getDate("NGAYSINH");
                    String s=""+date;
                    tb_qlms_ngaysinh.setText(s);
                    ktdg=true;
                }

                if(tb_qlms_hoten.equals("")) {
                    JOptionPane.showMessageDialog(this, "ko tồn tại");
                } else {
                    Calendar c=Calendar.getInstance();
                    day=c.get(Calendar.DAY_OF_MONTH);
                    month=c.get(Calendar.MONTH);
                    year =c.get(Calendar.YEAR);
                    ResultSet rs1=stm.executeQuery("select ngayhethan from The where The.MADOCGIA='" +tb_qlms_madg.getText() + "'");
                    while(rs1.next()) {
                        ngayhethan=rs1.getString("ngayhethan");
                        tb_qlms_ngayhethan.setText(ngayhethan);
                    }
                    yearhh=Integer.parseInt(ngayhethan.substring(0,4));
                    monthhh=Integer.parseInt(ngayhethan.substring(5,7));
                    dayhh=Integer.parseInt(ngayhethan.substring(8,10));
                    if (year > yearhh) {
                        JOptionPane.showMessageDialog(this, "Thẻ đã hết hạn sử dụng");
                        tb_qlms_mas.setVisible(false);
                        tb_qlms_mapm.setText("");
                        bt_qlms_chomuon.setVisible(false);
                    } else if (year == yearhh) {
                        if (month > monthhh) {
                            JOptionPane.showMessageDialog(this, "Thẻ đã hết hạn sử dụng");
                            tb_qlms_mas.setVisible(false);
                            tb_qlms_mapm.setText("");
                            bt_qlms_chomuon.setVisible(false);
                        } else if (month == monthhh) {
                            if (day > dayhh) {
                                JOptionPane.showMessageDialog(this, "Thẻ đã hết hạn sử dụng");
                                tb_qlms_mas.setVisible(false);
                                tb_qlms_mapm.setText("");
                                bt_qlms_chomuon.setVisible(false);
                            }
                        } else {
                            monthhh = monthhh + 3;
                            if (dayhh > 30) {
                                monthhh = monthhh + 1;
                                dayhh = dayhh - 30;
                            }
                            if (monthhh > 12 || monthhh + 3 > 12) {
                                monthhh = monthhh - 12;
                                year += 1;
                            }
                            if (year > yearhh) {
                                JOptionPane.showMessageDialog(this, "Thẻ sắp hết hạn sử dụng");
                                layphieumuon();
                                load_table();
                            } else if (year == yearhh) {
                                if (month > monthhh) {
                                    JOptionPane.showMessageDialog(this, "Thẻ sắp hết hạn sử dụng");
                                    layphieumuon();
                                    load_table();
                                } else if (month == monthhh) {
                                    if (day > dayhh) {
                                        JOptionPane.showMessageDialog(this, "Thẻ sắp hết hạn sử dụng");
                                        layphieumuon();
                                        load_table();
                                    }
                                }
                            }
                        }
                    } else {
                        layphieumuon();
                        load_table();
                    }
                }
                bt_qlms_ktdg.setVisible(false);
                tb_qlms_madg.setVisible(false);
                bt_qlms_ktsach.setVisible(true);
                tb_qlms_mas.setVisible(true);
            } catch (SQLException ex) {
                Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_bt_qlms_ktdgActionPerformed

    private void bt_qlms_ktsachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_qlms_ktsachActionPerformed
        // TODO add your handling code here:
        cleartextsach();
        bt_qlms_chomuon.setVisible(true);
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            try {
                Connection con = DriverManager.getConnection(getURLConnection());
                Statement stm=con.createStatement();
                ResultSet rs=stm.executeQuery("select TENSACH,HOTEN,TENLOAISACH,NGONNGU,GIA,TENNXB from SACH,LOAISACH,NGONNGU,NXB,TACGIA,SANGTAC where SACH.MASACH='" + tb_qlms_mas.getText() + "' AND SACH.MANGONNGU=NGONNGU.MANGONNGU AND SACH.MALOAISACH=LOAISACH.MALOAISACH AND SACH.MANXB=NXB.MANXB AND SACH.MASACH=SANGTAC.MASACH AND TACGIA.MATACGIA=SANGTAC.MATACGIA");
                while(rs.next()) {
                    tb_qlms_tensach.setText(rs.getString("TENSACH"));
                    tb_qlms_tacgia.setText(rs.getString("HOTEN"));
                    tb_qlms_linhvuc.setText(rs.getString("TENLOAISACH"));
                    tb_qlms_ngonngu.setText(rs.getString("NGONNGU"));
                    int i=rs.getInt("GIA");
                    tb_qlms_gia.setText(""+i);
                    tb_qlms_nhaxuatban.setText(rs.getString("TENNXB"));
                    kts=true;
                }
                if(tb_qlms_tensach.equals("")) {
                    JOptionPane.showMessageDialog(this, "Không Tồn tại");
                }

                else {
                    boolean sach=false;
                    String masach="";
                    ResultSet rs1=stm.executeQuery("SELECT SACH.masach FROM SACH,PHIEUMUON,SACH_PHIEU WHERE PHIEUMUON.MAPHIEU=SACH_PHIEU.MAPHIEU AND SACH_PHIEU.MASACH=SACH.MASACH AND SACH_PHIEU.NGAYTRA IS NULL");
                    while(rs1.next()) {
                        masach=rs1.getString("MASACH");
                        if(masach.trim().equals(tb_qlms_mas.getText())) {
                            sach=true;
                        }
                    }

                    if(sach) {
                        JOptionPane.showMessageDialog(this, "Sách này đã cho mượn");
                        bt_qlms_chomuon.setVisible(false);
                    }
                }


            } catch (SQLException ex) {
                Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_bt_qlms_ktsachActionPerformed

    private void bt_qlms_chomuonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_qlms_chomuonActionPerformed
        // TODO add your handling code here:
        if(ktdg&&kts) {
            Calendar c=Calendar.getInstance();
            ngaymuon=""+c.get(Calendar.MONTH)+"/"+c.get(Calendar.DAY_OF_MONTH)+"/"+c.get(Calendar.YEAR);
            int i=0;
            //tb_qlms_linhvuc.setText(ngaymuon);

            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                try {
                    Connection con = DriverManager.getConnection(getURLConnection());
                    Statement stm=con.createStatement();
                    //dua vao ten doc gia xem cac sach muon truoc do
                    ResultSet rs=stm.executeQuery("SELECT SACH.MASACH FROM SACH,DOCGIA,PHIEUMUON,SACH_PHIEU WHERE DOCGIA.MADOCGIA='"+tb_qlms_madg.getText()+"' AND DOCGIA.MADOCGIA=PHIEUMUON.MADOCGIA AND PHIEUMUON.MAPHIEU=SACH_PHIEU.MAPHIEU AND SACH_PHIEU.MASACH=SACH.MASACH AND SACH_PHIEU.NGAYTRA IS NULL");
                    while(rs.next()) {
                        //ko lay duoc kq
                        i++;
                    }
                    soluongmuon = i;
                    muontoida();
                    if (soluongmuon == sltoida)
                        JOptionPane.showMessageDialog(this, "Số lượng mượn đã tối đa");
                    else {
                        //cho muon tung quyen sach
                        ResultSet rs1=stm.executeQuery("SELECT MAPHIEU FROM DOCGIA,PHIEUMUON WHERE DOCGIA.MADOCGIA='" + tb_qlms_madg.getText() + "' AND DOCGIA.MADOCGIA=PHIEUMUON.MADOCGIA");
                        while(rs1.next()) {
                            if(phieutam.equals(rs1.getString("MAPHIEU").trim()))
                                taopm = false;
                        }
                        if(taopm)
                        {
                            phieumuon pm = new phieumuon();
                            pm.set_maPM(tb_qlms_mapm.getText());
                            pm.set_madg(tb_qlms_madg.getText());
                            pm.set_manv(home.manhanvien);
                            //Date ngaymuon1=Date.valueOf(ngaymuon);
                            pm.set_ngaymuon(ngaymuon);
                            if(pm.taomoiphieumuon()) {

                            } else JOptionPane.showMessageDialog(this, "có lỗi không tạo được phiếu mượn");
                        }
                        int daytra = c.get(Calendar.DAY_OF_MONTH);
                        int monthtra = c.get(Calendar.MONTH);
                        int yeartra = c.get(Calendar.YEAR);
                        if (monthtra + 3 > 12) {
                            monthtra = monthtra + 3 - 12;
                            yeartra += 1;
                        } else monthtra += 3;
                        ngayhethantra = "" + monthtra + "/" + daytra + "/" + yeartra;
                        //Date ngayhh = Date.valueOf(ngayhethantra);
                        phieusach ps = new phieusach();
                        ps.set_maphieumuon(tb_qlms_mapm.getText());
                        ps.set_masach(tb_qlms_mas.getText());
                        ps.set_ngayhethantra(ngayhethantra);
                        if (ps.taomoiphieusach()) {
                            soluongmuon++;
                            load_table();
                            JOptionPane.showMessageDialog(this, "Đã cho mượn sách ");
                        } else
                            JOptionPane.showMessageDialog(this, "có lỗi ko tạo được phiếu sách");
                    }

                } catch (SQLException ex) {
                    Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);
                }

            } catch (ClassNotFoundException ex) {
                Logger.getLogger(QLTV.class.getName()).log(Level.SEVERE, null, ex);

            }
        } else
            JOptionPane.showMessageDialog(this, "Phải kiểm tra thông tin trước khi cho mượn");
        bt_qlms_chomuon.setVisible(false);
}//GEN-LAST:event_bt_qlms_chomuonActionPerformed

    private void bt_qlms_huythaotacActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_qlms_huythaotacActionPerformed
        // TODO add your handling code here:
        tb_qlms_madg.setText("");
        tb_qlms_hoten.setText("");
        tb_qlms_doituong.setText("");
        tb_qlms_diachi.setText("");
        tb_qlms_ngaysinh.setText("");
        tb_qlms_gioitinh.setText("");
        tb_qlms_ngayhethan.setText("");
        tb_qlms_mapm.setText("");
        tb_qlms_mas.setText("");
        tb_qlms_tensach.setText("");
        tb_qlms_tacgia.setText("");
        tb_qlms_linhvuc.setText("");
        tb_qlms_ngonngu.setText("");
        tb_qlms_gia.setText("");
        tb_qlms_nhaxuatban.setText("");
        ktdg = false;
        kts = false;
        bt_qlms_chomuon.setVisible(false);
        bt_qlms_ktsach.setVisible(false);
        tb_qlms_mas.setVisible(false);
        bt_qlms_ktdg.setVisible(true);
        tb_qlms_madg.setVisible(true);
    }//GEN-LAST:event_bt_qlms_huythaotacActionPerformed
    private String getURLConnection() {
        return "jdbc:sqlserver://" + server + ":" + port +
                ";databaseName=" + databaseName +
                ";User=" + username +
                ";Password=" + password;
    }
    private void btthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btthoatActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btthoatActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new QLMUONSACH().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel PNktdg;
    private javax.swing.JPanel PNkts;
    private javax.swing.JButton bt_qlms_chomuon;
    private javax.swing.JButton bt_qlms_huythaotac;
    private javax.swing.JButton bt_qlms_ktdg;
    private javax.swing.JButton bt_qlms_ktsach;
    private javax.swing.JButton btthoat;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel40;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField tb_qlms_diachi;
    private javax.swing.JTextField tb_qlms_doituong;
    private javax.swing.JTextField tb_qlms_gia;
    private javax.swing.JTextField tb_qlms_gioitinh;
    private javax.swing.JTextField tb_qlms_hoten;
    private javax.swing.JTextField tb_qlms_linhvuc;
    private javax.swing.JTextField tb_qlms_madg;
    private javax.swing.JTextField tb_qlms_mapm;
    private javax.swing.JTextField tb_qlms_mas;
    private javax.swing.JTextField tb_qlms_ngayhethan;
    private javax.swing.JTextField tb_qlms_ngaysinh;
    private javax.swing.JTextField tb_qlms_ngonngu;
    private javax.swing.JTextField tb_qlms_nhaxuatban;
    private javax.swing.JTextField tb_qlms_tacgia;
    private javax.swing.JTextField tb_qlms_tensach;
    private javax.swing.JTable tb_xemkq;
    // End of variables declaration//GEN-END:variables

}
